import api from '@/api';

export default {
  namespace: 'service',

  state: {
    services: {
      list: [],
      pagination: {},
    },
    currentService: {},
    valueServices: [],
    transRequirement: []
  },

  effects: {
    *queryTransRequirement({ payload }, { call, put }) {
      let res = yield call(api.service.queryTransRequirement, payload)
      if (res.errorCode === 0) {
        yield put({
          type: 'saveTransRequirement',
          payload: res.data
        })
      }
    },
    *fetch({ payload }, { call, put }) {
      let res = yield call(api.service.query, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveServices',
          payload: {
            list: res.data,
            pagination: res.pagination,
          },
        });
      }
    },
    *fetchOne({ payload }, { call }) {
      return yield call(api.service.queryById, payload);
    },
    *fetchCurrent({ payload }, { call, put }) {
      const response = yield call(api.service.queryCurrent, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveCurrent',
          payload: response.data,
        });
      }
      return response
    },
    *add({ payload }, { call }) {
      return yield call(api.service.create, payload);
    },
    *update({ payload }, { call }) {
      return yield call(api.service.update, payload);
    },
    *delete({ payload }, { call }) {
      return yield call(api.service.remove, payload);
    },
    *submitAudit({ payload }, { call }) {
      return yield call(api.service.submitAudit, payload);
    },
    *audit({ payload }, { call }) {
      return yield call(api.service.audit, payload);
    },
    *start({ payload }, { call }) {
      return yield call(api.service.start, payload);
    },
    *stop({ payload }, { call }) {
      return yield call(api.service.stop, payload);
    },
    *fetchValueServices(_, { call, put }) {
      let res = yield call(api.service.queryValued);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveValueServices',
          payload: res.data,
        });
      }
      return res;
    },
    *fetchCalcRules({ payload }, { call, put }) {
      return yield call(api.service.queryCalcRuleList, payload);
    },
    *createCalcRules({ payload }, { call, put }) {
      return yield call(api.service.createCalcRule, payload);
    },
    *deleteCalcRule({ payload }, { call, put }) {
      return yield call(api.service.deleteCalcRule, payload);
    },
    *fetchOperateLog({ payload }, { call }) {
      const response = yield call(api.service.operateLog, payload);
      return response;
    },
  },

  reducers: {
    saveTransRequirement(state, { payload }) {
      return {
        ...state,
        transRequirement: payload,
      }
    },
    saveServices(state, { payload }) {
      return {
        ...state,
        services: payload,
      };
    },
    saveCurrent(state, { payload }) {
      return {
        ...state,
        currentService: payload,
      };
    },
    clearCurrent(state) {
      return {
        ...state,
        currentService: {},
      };
    },
    saveValueServices(state, { payload }) {
      return {
        ...state,
        valueServices: payload,
      };
    },
  },
};
